/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.entity.DrugInfo;
import com.jims.master.modules.drug.service.DrugInfoService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 药品基本信息Controller
 * @author CTQ
 * @version 2017-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugInfo")
public class DrugInfoController extends BaseController {

	@Autowired
	private DrugInfoService drugInfoService;
	
	@ModelAttribute
	public DrugInfo get(@RequestParam(required=false) String id) {
		DrugInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drugInfoService.get(id);
		}
		if (entity == null){
			entity = new DrugInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(DrugInfo drugInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrugInfo> page = drugInfoService.findPage(new Page<DrugInfo>(request, response), drugInfo); 
		model.addAttribute("page", page);
		model.addAttribute("drugInfo",drugInfo);
		return "modules/drug/drugInfoIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(DrugInfo drugInfo, Model model) {
		model.addAttribute("drugInfo", drugInfo);
		return "modules/drug/drugInfoForm";
	}

    @RequiresPermissions("user")
    @RequestMapping(value = "findByDrugId")
    @ResponseBody
    public StringData findByDrugId(DrugInfo drugInfo, Model model) {
        StringData data=new StringData();
        int i=drugInfoService.findByDrugId(drugInfo);
        if(i<1){
            data.setCode("success");
            data.setData("保存药品基本信息成功");
        }else{
            data.setCode("error");
            data.setData("保存药品基本信息失败");
        }
        return data;
    }

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DrugInfo drugInfo, Model model) {
	    StringData data=new StringData();
		int i=drugInfoService.save(drugInfo);
        if(i>0){
            data.setCode("success");
            data.setData("保存药品基本信息成功");
        }else{
            data.setCode("error");
            data.setData("保存药品基本信息失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(DrugInfo drugInfo) {
	    StringData data=new StringData();
		int i=drugInfoService.delInfo(drugInfo);
        if(i>0){
            data.setCode("success");
            data.setData("删除药品基本信息成功");
        }else if(i==-1){
			data.setCode("info");
			data.setData("该药品已有药品价格，不能删除");
		}else{
            data.setCode("error");
            data.setData("删除药品基本信息失败");
        }
		return data;
	}

	/**
	 * @param drugInfo 传递参数
	 * @return List<DrugDict>    返回类型
	 * @throws
	 * @Title: autoComplete
	 * @Desciption: (药品基本信息添加药品字典自动补全)
	 * @author CTQ
	 * @date 2017-03-02
	 */
	@RequiresPermissions("user")
	 @RequestMapping(value = {"autoComplete", ""})
	 @ResponseBody
	 public List<DrugInfo> autoComplete(String q,String limit, DrugInfo drugInfo) {
		if(q!=null&&!q.equals("")){
			try {
				q = URLDecoder.decode(q,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List<DrugInfo> list = drugInfoService.findAutoCompleteList(drugInfo, "",q,limit);
		return list;
	}



    /**
     * @param drugInfo 传递参数
     * @return List<DrugInfo>    返回类型
     * @throws
     * @Title: autoCompletePrice
     * @Desciption: (药品入库选存在价格的药品自动补全,只选择对应的大包装)
     * @author ZHUQ
     * @date 2017-03-12
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoCompletePrice", ""})
    @ResponseBody
    public List<DrugInfo> autoCompletePrice(String q,String limit, DrugInfo drugInfo) {
        if(q!=null&&!q.equals("")){
            try {
                q = URLDecoder.decode(q,"UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<DrugInfo> list = drugInfoService.findAutoCompletePriceist(drugInfo, UserUtils.getOrgId(), q, limit);
        return list;
    }

//    /**
//     * @param drugInfo 传递参数
//     * @return List<DrugInfo>    返回类型
//     * @throws
//     * @Title: autoCompletePrice
//     * @Desciption: (药品入库选存在价格的药品自动补全,关联药品价格视图,查回进价售价信息)
//     * @author ZYG
//     * @date 2017-07-20
//     */
//    @RequiresPermissions("user")
//    @RequestMapping(value = {"autoCompletePriceNew", ""})
//    @ResponseBody
//    public List<DrugInfo> autoCompletePriceNew(String q,String limit, DrugInfo drugInfo) {
//        if(q!=null&&!q.equals("")){
//            try {
//                q = URLDecoder.decode(q,"UTF-8");
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//        }
//        List<DrugInfo> list = drugInfoService.findAutoCompletePriceistNew(drugInfo, UserUtils.getOrgId(), q, limit);
//        return list;
//    }

}